package com.imooc.security.browser;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

/**
 * 用户信息认证与校验逻辑
 * @author Jon
 *
 */
@Component
public class MyUserDetailService implements UserDetailsService{
	
	@Autowired
	PasswordEncoder passwordEncoder;

	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		// 根据用户名从数据库中获取密码
		System.out.println("用户名：" + username);
		String password = passwordEncoder.encode("123456");
		System.out.println("密码加密后：" + password);
		// 将用户名与数据库中获取的密码传入参数，SpringSecurity会自动校验用户输入的密码是否匹配
		return new User(username, password, 
				true,true,true,true,
				AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));
	}

}
